package com.shuai.jit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuai.jit.entity.R;
import com.shuai.jit.entity.user.Permission;
import com.shuai.jit.entity.user.User;
import com.shuai.jit.mapper.PermissionMapper;
import com.shuai.jit.mapper.UserMapper;
import com.shuai.jit.service.impl.LoginServiceImpl;
import com.shuai.jit.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api("用户控制类")
@RequestMapping("/user")
public class LoginController {

    @Autowired
    LoginServiceImpl loginService;

    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    UserMapper userMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;


    /**
     * 使用电话号码+ 验证码登录
     * @param telephone
     * @param code
     * @return
     */
    @RequestMapping("/telephoneCode")
    public R telephoneCode(String telephone, String code){
        Map<String,Object> map=new HashMap<>();
        String token=loginService.loginTelephoneCode(telephone,code);
        if(token!=null){
            //生成token
            map.put("token",token);
            return R.ok().data(map);
        }else {
            map.put("error","对不起验证码或者手机号问题");
            return R.error().data(map);
        }

    }

    @ApiOperation("使用学校进行登录")
    @RequestMapping("/numberPassword")
    public R numberPassword(String number, String password){
        Integer result=loginService.loginNumberPassword(number,password);
        if(result==1){
            QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
            userQueryWrapper.eq("number",number);
            User user=userMapper.selectOne(userQueryWrapper);
            List<String> authorityList = new ArrayList<>();
            // 这里读取权限表，然后查看
            // 根据用户的等级，获取全部的权限
            List<Permission> permissions=permissionMapper.selectByLev(user.getUserLev());
            // 添加路径
            for (Permission p:permissions){
                authorityList.add(p.getPath());
            }
            // 设置权限
            redisTemplate.opsForValue().set(user.getUsername(),authorityList);
            return R.ok().data("token",jwtUtil.sign(user.getUsername()));
        }else if(result==-1){
            return R.error().data("token","学号不存在");
        }else {
            return R.error().data("token","密码错误");
        }
    }


    @RequestMapping("/test")
    public R test(){
        Map<String,Object> map=new HashMap<>();
        map.put("test","成功");
        return R.ok().data(map);
    }

    /**
     * 发送验证码
     * @param phone 手机号
     * @return
     */
    @ApiOperation("发送验证码")
    @RequestMapping("/send")
    public R send(String phone){
        return R.ok().data("code",loginService.send(phone));
    }
}
